package com.matt.repository;

import com.matt.domain.Clientele;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * *********************
 * Created by Ron
 * from com.matt
 * on 2018/6/14 0014.
 * ********************
 */
public interface ClienteleRepository extends JpaRepository<Clientele, String>,JpaSpecificationExecutor<Clientele> {

    Clientele findFirstByAreaIdAndId(String areaId, String id);

    Clientele findBySysChildNameAndAreaId(String sysChildName,String areaId);

    Clientele findByChildNameAndAreaIdAndDeletedAndMobile(String sysChildName,String areaId,boolean delete, String mobile);

    List<Clientele> findByChildNameAndAreaId(String childName,String areaId);

    List<Clientele> findByChildNameAndAreaIdAndDeleted(String childName,String areaId,Boolean delete);

    List<Clientele> findByLeadsIdAndDeleted(String leadsId, Boolean deleted);

    Clientele findFirstByAreaIdAndChildNameAndMobile(String areaId, String childName, String mobile);

    @Transactional
    @Modifying
    @Query(value = "update clientele set status=:status,statusVal=:statusVal,lastFollowRecord=:lastFollowRecord,lastFollowTime=NOW() where id=:clientelesId",nativeQuery = true)
    int updateStatus(@Param("clientelesId") String clientelesId,@Param("status") Integer status,@Param("statusVal") String statusVal,@Param("lastFollowRecord") String lastFollowRecord);

    @Query(value = "SELECT * FROM `clientele` where areaId  in (:areaIds) and DATE_FORMAT(created,'%Y-%m') = DATE_FORMAT(:created,'%Y-%m')",nativeQuery = true)
    List<Clientele> findByAreaIdAndCreated(@Param("areaIds")String areaIds,@Param("created")String created);
}
